﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace AdornerPopup.Core.Mvvm
{
    public class RelayCommand<T> : ICommand where T : class
    {
        private readonly static Predicate<T> AlwayseTrue = _ => true;

        private Action<T> _execute;
        private Predicate<T> _canExecute;

        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        public RelayCommand(Action<T> execute)
            : this(execute, null)
        { }

        public RelayCommand(Action<T> execute, Predicate<T> canExecute)
        {
            this._execute = execute;
            this._canExecute = canExecute ?? AlwayseTrue;
        }

        public bool CanExecute(object parameter)
        {
            return _canExecute.Invoke(parameter as T);
        }

        public void Execute(object parameter)
        {
            _execute.Invoke(parameter as T);
        }
    }

}
